
package com.android.sharecar.activity;

import com.android.sharecar.model.FavoriteAddress;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

/**
 * 开车界面
 * 
 * @author xinyan
 * @date 2011-9-13
 */
public class ProvideRideAcitvity extends Activity {
    public static final String TAG = "DrivingAcitvity";

    public static final int REQUEST_CODE_ADDRESS_START = 1;

    public static final int REQUEST_CODE_ADDRESS_END = 3;

    // Views
    private Button mSubmitDriveInfo, mRequestList;

    private EditText mStartPlace, mEndPlace;

    private ImageButton mStartPlaceFavorite, mEndPlaceFavorite;

    private Spinner mProvideRideNum;

    private RadioGroup mChooseRoute;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.provideride);

        findViews();

        setListenerForViews();
        setTitlebar();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void setTitlebar() {
        /*
         * MainTabWidgetActivity.mInstance.getWindow().setFeatureInt(Window.
         * FEATURE_CUSTOM_TITLE, R.layout.titilebar_provideride);
         */

        mSubmitDriveInfo = (Button) findViewById(R.id.providerideactivity_btn_submit);
        mRequestList = (Button) findViewById(R.id.providerideactivity_btn_requestList);

        mSubmitDriveInfo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });

        mRequestList.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ProvideRideAcitvity.this, PickRideRequestListActivity.class);
                // intent.putExtra(StandardActivity.STATE,
                // StandardActivity.STATE_WAITING_FOR_RIDE);
                startActivity(intent);
            }
        });
    }

    private void findViews() {
        mEndPlace = (EditText) findViewById(R.id.providerideactivity_et_endPlace);
        mStartPlace = (EditText) findViewById(R.id.providerideactivity_et_startPlace);
        mProvideRideNum = (Spinner) findViewById(R.id.providerideactivity_sp_provideRideNum);
        mStartPlaceFavorite = (ImageButton) findViewById(R.id.providerideactivity_ib_startPlace_favorite);
        mEndPlaceFavorite = (ImageButton) findViewById(R.id.providerideactivity_ib_endPlace_favorite);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.select_dialog_vacantSeatNum, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mProvideRideNum.setAdapter(adapter);
    }

    private void setListenerForViews() {
        mStartPlaceFavorite.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(ProvideRideAcitvity.this,
                        FavoriteAddressActivity.class), REQUEST_CODE_ADDRESS_START);
            }
        });

        mEndPlaceFavorite.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(ProvideRideAcitvity.this,
                        FavoriteAddressActivity.class), REQUEST_CODE_ADDRESS_END);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_ADDRESS_END:
                if (resultCode == RESULT_OK) {
                    String address;
                    Bundle resultBundle = data.getExtras();
                    if (resultBundle.containsKey(FavoriteAddress.TAG)) {
                        address = resultBundle.getString(FavoriteAddress.TAG);
                    } else {
                        break;
                    }
                    mEndPlace.setText(address);
                }
                break;
            case REQUEST_CODE_ADDRESS_START:
                if (resultCode == RESULT_OK) {
                    String address;
                    Bundle resultBundle = data.getExtras();
                    if (resultBundle.containsKey(FavoriteAddress.TAG)) {
                        address = resultBundle.getString(FavoriteAddress.TAG);
                    } else {
                        break;
                    }
                    mStartPlace.setText(address);
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
